package com.gas.project.gas.teacher.controller;

import java.util.List;

import com.gas.common.utils.DateUtils;
import com.gas.common.utils.StringUtils;
import com.gas.common.utils.poi.ExcelUtil;
import com.gas.framework.aspectj.lang.annotation.Log;
import com.gas.framework.aspectj.lang.enums.BusinessType;
import com.gas.framework.web.controller.BaseController;
import com.gas.framework.web.domain.AjaxResult;
import com.gas.framework.web.page.TableDataInfo;
import com.gas.project.gas.student.domain.Student;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gas.project.gas.teacher.domain.Teacher;
import com.gas.project.gas.teacher.service.ITeacherService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教职工信息Controller
 *
 * @author xyl
 * @date 2022-04-02
 */
@Controller
@RequestMapping("/gas/teacher")
public class TeacherController extends BaseController {
    private String prefix = "gas/teacher";

    @Autowired
    private ITeacherService teacherService;

    @RequiresPermissions("gas:teacher:view")
    @GetMapping()
    public String teacher() {
        return prefix + "/teacher";
    }

    /**
     * 查询教职工信息列表
     */
    @RequiresPermissions("gas:teacher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Teacher teacher) {
        startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }

    /**
     * 导出教职工信息列表
     */
    @RequiresPermissions("gas:teacher:export")
    @Log(title = "教职工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Teacher teacher) {
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        return util.exportExcel(list, "teacher");
    }

    /**
     * 新增教职工信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存教职工信息
     */
    @RequiresPermissions("gas:teacher:add")
    @Log(title = "教职工信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Teacher teacher) {
        return toAjax(teacherService.insertTeacher(teacher));
    }

    /**
     * 修改教职工信息
     */
    @GetMapping("/edit/{teacherNumber}")
    public String edit(@PathVariable("teacherNumber") String teacherNumber, ModelMap mmap) {
        Teacher teacher = teacherService.selectTeacherById(teacherNumber);
        mmap.put("teacher", teacher);
        return prefix + "/edit";
    }

    /**
     * 修改保存教职工信息
     */
    @RequiresPermissions("gas:teacher:edit")
    @Log(title = "教职工信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Teacher teacher) {
        return toAjax(teacherService.updateTeacher(teacher));
    }

    /**
     * 删除教职工信息
     */
    @RequiresPermissions("gas:teacher:remove")
    @Log(title = "教职工信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teacherService.deleteTeacherByIds(ids));
    }

    @RequiresPermissions("gas:teacher:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<Teacher> util = new ExcelUtil<>(Teacher.class);
        return util.importTemplateExcel("教师数据");
    }


    @Log(title = "教师管理" , businessType = BusinessType.IMPORT)
    @RequiresPermissions("gas:teacher:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Teacher> util = new ExcelUtil<>(Teacher.class);
        List<Teacher> teacherList = util.importExcel(file.getInputStream());
        //数据校验
        for (Teacher teacher : teacherList) {
            teacher.setCreateBy(getLoginName());
            teacher.setCreateTime(DateUtils.getNowDate());
            String check = teacher.check(teacher);
            if(StringUtils.isNotEmpty(check)){
                return error(check);
            }
        }
        String message = teacherService.importTeacher(teacherList);
        return AjaxResult.success(message);
    }
}
